package logic

import (
	"baidu/modil/cache"
	"baidu/modil/mysql"
	"context"
	"fmt"

	"baidu/baidurpc/baidurpc"
	"baidu/baidurpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PwdUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPwdUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PwdUpdateLogic {
	return &PwdUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PwdUpdateLogic) PwdUpdate(in *baidurpc.PwdUpdateRequest) (*baidurpc.PwdUpdateResponse, error) {
	// todo: add your logic here and delete this line
	get := cache.Rdb.Get(l.ctx, "sendSms"+in.Mobile)
	if get.Val() != in.SmsCode {
		return nil, fmt.Errorf("短信验证码错误,请重新输入")
	}
	var user mysql.User
	mysql.DB.Debug().Where("id=?", in.Id).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("该账号不存在")
	}
	user.Password = in.Password
	mysql.DB.Debug().Updates(&user)
	return &baidurpc.PwdUpdateResponse{
		UserId: int64(user.ID),
	}, nil
}
